using System;
using System.Data;
using System.Web;
using System.Web.Caching;
using System.Data.SqlClient;
using PetShop.Web.Entities;

namespace PetShop.Web.Controls{

	


	
	
	
	public partial  class Banner : System.Web.UI.UserControl{

       
   
		protected void Page_Load(object sender, System.EventArgs e) {
			ShowBanner();
		}


  
     
		
		private void ShowBanner() {

			if (Request.IsAuthenticated == true){

				
				
				AccountInfo myAccount = GetAccountInfo(false);

				if (myAccount != null) {
					areaBanner.Visible = myAccount.IsShowBanners;
				
					string categoryKey = myAccount.Category;
					string bannerKey = "Banner" + categoryKey;
					string bannerPath = "";

					if(Cache[bannerKey] != null){
						
						bannerPath = ( (string)Cache[bannerKey] );
					}else{
						
						 bannerPath = GetBannerPath(categoryKey);

						
						Cache.Add(bannerKey, bannerPath, null, DateTime.Now.AddHours(6), Cache.NoSlidingExpiration , CacheItemPriority.High, null);
					}

					areaImage.InnerHtml = bannerPath;
				}
			}
		}


        private AccountInfo GetAccountInfo(bool required)
        {
            AccountInfo myAccount = (AccountInfo)HttpContext.Current.Session["ACCOUNT_KEY"];

            if (myAccount == null)
            {
                if (required)
                {
                    HttpContext.Current.Response.Redirect("WebFormSignIn.aspx", true);

                }
                return null;
            }
            else
            {
                return myAccount;
            }
        }

        private string GetBannerPath(string categoryKey)
        {
            if (string.IsNullOrEmpty(categoryKey)) return null;


            CategoryInfo cat = GetCategoryByID(categoryKey);

            string profileGetBannerPath = cat.BannerData;

            return profileGetBannerPath;

        }

        public CategoryInfo GetCategoryByID(string categoryID)
        {

            CategoryInfo cat = null;

            string sql = "SELECT [CatId] ,[Name]  ,[Descn]  ,[BannerData]  FROM [dbo].[Category] where  catId = @catId";

            SqlParameter parm = new SqlParameter("@catId", SqlDbType.VarChar, 10);

            parm.Value = categoryID;


            using (SqlCommand cmd = new SqlCommand())
            {
                using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["SQLConnString"]))
                {
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add(parm);
                    conn.Open();
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (rdr.Read())
                        {
                            cat = new CategoryInfo();
                            cat.CategoryID = rdr.GetString(0);
                            cat.Name = rdr.GetString(1);
                            cat.Description = rdr.GetString(2);
                            cat.BannerData = rdr.GetString(3);
                        }


                        return cat;
                    }
                }
            }
        }


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			
			
			
			InitializeComponent();
			base.OnInit(e);
		}
		
		
		
		
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
